﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.ServiceModel;
using PeertoPeerObjectShare.PacketHandler;

namespace PeertoPeerObjectShare
{
    class Program : IPeerChannelChat
    {
        public string Name { get; set; }

        static void Main(string[] args)
        {

            Console.WriteLine("Enter Your name");
            string member = Console.ReadLine();

            InstanceContext context = new InstanceContext(new Program() { Name = member });

            DuplexChannelFactory<IPeerChannelChatClient> factory = new DuplexChannelFactory<IPeerChannelChatClient>(context, "ChatChannel");
            IPeerChannelChatClient client = factory.CreateChannel();

            IOnlineStatus onlinestatus = client.GetProperty<IOnlineStatus>();
            onlinestatus.Online += new EventHandler(onlinestatus_Online);
            onlinestatus.Offline += new EventHandler(onlinestatus_Offline);

            Console.Write("Opening Chat Client.....");
            client.Open();
            Console.WriteLine("OPENED!");


            client.Logon(member);

            while (true)
            {
                PackageHandler sender = new PackageHandler();
                Console.Write(" \nPress: 1: Chat \t\n 2: Comment \t\n 3: Link \t\n 4: Wallpost ");

                string type = Console.ReadLine();

                Console.WriteLine("Content: " );
                string message = Console.ReadLine();
                if (message.ToUpper() == "QUIT") break;

                switch (type)
                {

                    case("1"):
                        sender = PackageCreator.getPackage("chat", "to", member, message);
                        break;

                    case("2"):
                        sender = PackageCreator.getPackage("comment", "to", member, message);
                        break;

                    case("3"):
                        sender = PackageCreator.getPackage("link", "to", member, message);
                        break;

                    case("4"):
                        sender = PackageCreator.getPackage("wallpost", "to", member, message);
                        break;
                }

               


                string jsonmsg = sender.toJSON();
               
                client.Chat(member, jsonmsg);
            }

            client.Logoff(member);
            client.Close();
            factory.Close();


        }


        #region OnlineStatus


        static void onlinestatus_Online(object o, EventArgs e )
        {
            Console.WriteLine("<<<<<< CHAT CLIENT IS ONLINE");
        }

        static void onlinestatus_Offline(object o, EventArgs e)
        {
            Console.WriteLine("<<<<<< CHAT CLIENT IS OFFLINE");
        }

        #endregion


        #region IPeerChannelChat Members
        public void Logon(string name)
        {
            if(name != this.Name)
            Console.WriteLine("<<<<<<-----{0} Joined",name.ToUpper());
        }

        public void Chat(string name, string message)
        {
            if (name != this.Name)
            {
                PackageHandler packet = PacketResolver.get_Package(message);
                //Console.WriteLine("Type of Package Recieved: {0}",packet.GetType().ToString()); 
                Console.WriteLine("{0} Says: {1}: {2}", name.ToUpper(), packet.packtype, packet.message);
            }
        }

        public void Logoff(string name)
        {
            if (name != this.Name)
            Console.WriteLine("<<<<<<-----{0} Exited", name.ToUpper());
        }
        #endregion
    }
}
